﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Data.Linq;
using WX.WMS.DataModel;
using WX.WMS.Common;
using WX.WMS.BLL;

public partial class Transfer_TransStockAudit : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["SortOrder"] = "CREATE_TIME";
            ViewState["OrderDire"] = "Asc";

            DataBandStatusList();

            BindGridView();
        }

    }

    protected void BindGridView()
    {
        TransferBLL bll = new TransferBLL();

        if (this.CreateUser.Text.Trim() != "")
        {
            bll.AddLikeCondition("T_SYS_USER.NAME", this.CreateUser.Text.Trim());
        }
        if (this.CreateDateFrom.Text.Trim() != "" && this.CreateDateTo.Text.Trim() != "")
        {
            bll.AddBetweenCondition("CREATE_TIME", this.CreateDateFrom.Text.Trim(), this.CreateDateTo.Text.Trim(), true);
        }
        if (this.ddlStatus.SelectedValue != "-1")
        {
            bll.AddEqualCondition("STATE", this.ddlStatus.SelectedValue);
        }

        bll.AddOrderByCondition(ViewState["SortOrder"] + " " + ViewState["OrderDire"]);

        this.gvTransStockList.DataSource = bll.GetTransferList();
        this.gvTransStockList.DataBind();
    }

    protected void gvTransStockList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.gvTransStockList.PageIndex = e.NewPageIndex;
        BindGridView();
    }
    protected void btnAudit_Click(object sender, EventArgs e)
    {
        ArrayList list = this.gvTransStockList.GetCheckedList();
        if (list != null && list.Count > 0)
        {
            TransferBLL transferBLL = new TransferBLL();
            foreach (string id in list)
            {
                T_TRANSFER transfer = transferBLL.GetTransferByTransferID(id); ;

                if (transfer.STATE == (int)Enumerations.StockStatus.审核通过)
                {
                    ShowMessage("调仓单 " + transfer.TRANSFER_ID + " 已被审核，不能再审核！");
                    return;
                }
                string Message = string.Empty;
                if (transferBLL.AuditTransfer(id, this.UserInfo.ID,out Message) == -1)
                {
                    ShowMessage("调仓单 " + transfer.TRANSFER_ID + " 不能被审核！因为"+Message+"！");
                    return;
                }
            }

            ShowMessage("审核成功！");
            BindGridView();
            return;
        }
        else
        {
            ShowMessage("请至少选择一个单据！");
            return;
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        ArrayList list = this.gvTransStockList.GetCheckedList();
        if (list != null && list.Count > 0)
        {
            TransferBLL transferBLL = new TransferBLL();
            foreach (string id in list)
            {
                T_TRANSFER transfer = transferBLL.GetTransferByTransferID(id); ;

                if (transfer.STATE == (int)Enumerations.StockStatus.审核通过)
                {
                    ShowMessage("调仓单 " + transfer.TRANSFER_ID + " 已被审核，不能打回！");
                    return;
                }

                if (transferBLL.BackTransfer(id) == -1)
                {
                    ShowMessage("调仓单 " + transfer.TRANSFER_ID + " 不能被打回！请重试！");
                    return;
                }
            }

            ShowMessage("打回成功！");
            BindGridView();
            return;
        }
        else
        {
            ShowMessage("请至少选择一个单据！");
            return;
        }
    }

    protected void DataBandStatusList()
    {
        CommomMethod.BindEnumForDropdwonList(this.ddlStatus, typeof(Enumerations.StockStatus));
        this.ddlStatus.Items.Insert(0, new ListItem("请选择", "-1"));
    }
    protected void searchButton_Click(object sender, ImageClickEventArgs e)
    {
        BindGridView();
    }
}
